package com.itcg.library.controller;

import com.itcg.library.pojo.Books;
import com.itcg.library.pojo.BooksPageQuery;
import com.itcg.library.pojo.PageBean;
import com.itcg.library.pojo.Result;
import com.itcg.library.service.BooksService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/books")
public class BooksController {
    @Autowired
    private BooksService booksService;

    @PostMapping
    public Result insert(@RequestBody Books books) {
        booksService.insert(books);
        return Result.success();
    }

    @DeleteMapping("/{ids}")
    public Result batchDelete(@PathVariable List<Integer> ids) {
        booksService.batchDelete(ids);
        return Result.success();
    }

    @PutMapping
    public Result updates(@RequestBody Books books) {
        booksService.updates(books);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        Books books = booksService.findById(id);
        return Result.success(books);
    }

    @GetMapping
    public Result findByConditions(BooksPageQuery booksPageQuery) {
        PageBean<Books> page = booksService.findByConditions(booksPageQuery);
        log.info("{}", page);
        return Result.success(page);
    }

    @GetMapping("/findByConditions")
    public Result findByConditions2(BooksPageQuery booksPageQuery) {
        PageBean<Books> page = booksService.findByConditions2(booksPageQuery);
        return Result.success(page);
    }


}